package com.shuimin.as.util;

import pond.common.JSON;
import pond.common.STRING;
import pond.web.Response;

import java.util.HashMap;
import java.util.Map;

/**
 * 生成resp错误内容
 * Created by Administrator on 2015/8/12.
 */
public class RespUtil {

    /**
     * 格式：
     * {
     * code:403,
     * message:"XXXXXXXXX"
     * }
     *
     * @param code
     * @param codeDesc
     * @return
     */
    public static void error(Response resp, int code, int codeDesc, String... errors) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", codeDesc);
        String message = "";
        if (errors != null && errors.length > 0) {
            message = errors[0];
        }
        map.put("message", message + ErrorMessageUtil.get(codeDesc));
        resp.contentType("application/json;charset=utf-8");
        resp.write(JSON.stringify(map));
        resp.send(code);
    }

    /**
     * success
     * 格式:
     * {
     *     code:200,
     *     message:"XXXXXX"
     * }
     * @param resp
     * @param msg
     */
    public static void success(Response resp,String msg){
        if(STRING.isBlank(msg)){
            msg="success";
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("message",msg);
        resp.contentType("application/json;charset=utf-8");
        resp.write(JSON.stringify(map));
        resp.send(200);
    }
}
